﻿using Mybook.Models;
using MyStory.Service;
using MyStory.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Mybook.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Book : ContentPage
    {
        private FictionService _fictionService;
        public Book()
        {
            InitializeComponent();
            _fictionService = new FictionService();
            init();
            
            
        }

        private async void init()
        {
            List<FictionModel> repositories = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title","王"));
            collectionView.ItemsSource = repositories;
        }

        private async void OnButtonClicked(object sender, EventArgs e)
        {
            List<FictionModel> repositories0= new List<FictionModel>();
            List<FictionModel> repositories = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title", text.Text));
            List<FictionModel> repositories1 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("author", text.Text));
            List<FictionModel> repositories2 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("fictionType", text.Text));
            if (repositories != null)
            {
                repositories0.AddRange(repositories);
            }
            if (repositories1!=null)
            {
                repositories0.AddRange(repositories1);
            }
            if (repositories2 != null)
            {
                repositories0.AddRange(repositories2);
            }        

            collectionView.ItemsSource = repositories0;
        }
        private async void OnButtonClicked1(object sender, EventArgs e)
        {
            List<FictionModel> repositories0 = new List<FictionModel>();
            List<FictionModel> repositories = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title", text.Text,size:1000));
            List<FictionModel> repositories1 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("author", text.Text, size: 1000));
            List<FictionModel> repositories2 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("fictionType", text.Text, size: 1000));
            if (repositories != null)
            {
                repositories0.AddRange(repositories1);
            }
            if (repositories1 != null)
            {
                repositories.AddRange(repositories1);
            }
            if (repositories2 != null)
            {
                repositories.AddRange(repositories2);
            }
            collectionView.ItemsSource = repositories;
            
            
            
        }
        public static string fictionid;

        async void OnTapGestureRecognizerTapped(object sender,EventArgs e)
        {
            await Navigation.PushAsync(new SearchDetailPageDetail());
        }
        ViewCell lastCell;
        private async void collectionView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            //DisplayAlert("xaio", e.SelectedItem.ToString(),"aa" );
            //DisplayAlert("xaio", model.FictionId, "aa");
            if (e.SelectedItem == null) return;  //The selection is set to null in some cases
            if (!(sender is ListView listView)) return;
            if (!(listView.TemplatedItems[0] is ViewCell cell)) return;
            FictionModel model = (FictionModel)e.SelectedItem;
            await Navigation.PushAsync(page: new BookDetail(model)); 
        }
        private void ViewCell_Tapped(object sender, EventArgs e)
        {
            if (lastCell != null)
                lastCell.View.BackgroundColor = Color.AntiqueWhite;
            var viewCell = (ViewCell)sender;
            if (viewCell.View != null)
            {
                viewCell.View.BackgroundColor = Color.AliceBlue;
                lastCell = viewCell;
            }
        }
        public void OnMore(object sender, EventArgs e)
        {
            var mi = ((MenuItem)sender);
            DisplayAlert("More Context Action", mi.CommandParameter + " more context action", "OK");
        }

        public void OnDelete(object sender, EventArgs e)
        {
            var mi = ((MenuItem)sender);
            DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK");
        }


    }
}